package com.learn.design.singleton.test03;


//懒汉式（线程安全）
public class User {

    private static User user;

    private User(){}

    public static synchronized User getInstance(){
        if(user == null){
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            user = new User();
        }
        return  user;
    }

}

class Test{
    public static void main(String[] args) {
     /*   User user = User.getInstance();
        User user1 = User.getInstance();
        User user2 = User.getInstance();
        System.out.println(user==user1);
        System.out.println(user==user2);
        System.out.println(user.hashCode());
        System.out.println(user1.hashCode());
        System.out.println(user2.hashCode());*/

        for (int i = 0; i < 100; i++) {
           new Thread(() ->
                    System.out.println(User.getInstance().hashCode())
            ).start();
        }
    }
}
